package com.santu.edu.supporter.biz.good.dao;

import com.santu.edu.supporter.biz.good.dao.mapper.GoodMapper;
import com.santu.edu.supporter.biz.good.pojo.dto.GoodDTO;
import com.santu.edu.supporter.biz.good.pojo.po.GoodPO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author luoyaoqi
 * @createTime 2024/3/12
 * @description
 */
@Repository
@RequiredArgsConstructor
public class GoodDao {

    private final GoodMapper goodMapper;

    public void addGood(GoodDTO dto) {
        GoodPO item = GoodPO.createItem(dto);
        goodMapper.insertOne(item);
    }

    public void updateGood(GoodDTO dto) {
        GoodPO item = GoodPO.createItem(dto);
        goodMapper.updateOne(item);
    }

    public void removeGood(String goodId) {
        goodMapper.removeOne(goodId);
    }

    public List<GoodDTO> getGoodList(String categoryId) {
        return goodMapper.selectGoodList(categoryId);
    }

    public List<GoodDTO> getGoodByIdList(List<String> goodIdList) {
        List<GoodPO> itemList = goodMapper.selectGoodByIdList(goodIdList);
        return itemList.stream().map(GoodDTO::createItem).collect(Collectors.toList());
    }

    public void updateGoodNum(String goodId, int updateNum) {
        goodMapper.updateGoodNum(goodId, updateNum);
    }
}
